using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using JGSY.CMS.LowCode.Platform.Application.Interfaces;
using JGSY.CMS.LowCode.Platform.Application.DTOs;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JGSY.CMS.LowCode.Platform.Controllers
{
    /// <summary>
    /// 用户角色管理接口。
    /// 提供用户与角色的关联管理相关 API。
    /// </summary>
    [ApiController]
    [Route("api/user-roles")]
    [Authorize(Roles = "admin")]
    public class UserRoleController : ControllerBase
    {
        /// <summary>
        /// 用户角色应用服务接口
        /// </summary>
        private readonly IUserRoleAppService _service;
        /// <summary>
        /// 构造函数，注入用户角色服务
        /// </summary>
        /// <param name="service">用户角色应用服务接口</param>
        public UserRoleController(IUserRoleAppService service)
        {
            _service = service;
        }
        /// <summary>
        /// 获取所有用户角色关联信息
        /// </summary>
        /// <returns>用户角色列表</returns>
        [HttpGet]
        public async Task<IEnumerable<UserRoleDto>> GetAll() => await _service.GetAllAsync();

        /// <summary>
        /// 添加用户角色关联
        /// </summary>
        /// <param name="dto">用户角色数据传输对象</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<IActionResult> Add(UserRoleDto dto)
        {
            await _service.AddAsync(dto);
            return Ok();
        }

        /// <summary>
        /// 移除用户角色关联
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <param name="roleId">角色主键</param>
        /// <returns>操作结果</returns>
        [HttpDelete]
        public async Task<IActionResult> Remove(long userId, long roleId)
        {
            await _service.RemoveAsync(userId, roleId);
            return Ok();
        }

        /// <summary>
        /// 获取指定用户的所有角色
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <returns>角色列表</returns>
        [HttpGet("user/{userId}/roles")]
        public async Task<IEnumerable<RoleDto>> GetRolesByUser(long userId)
            => await _service.GetRolesByUserAsync(userId);
    }
}
